package com.znsd.zl.redis.controller;

import com.znsd.zl.vo.req.RedisReqVO;
import com.znsd.zl.utils.R;
import com.znsd.zl.redis.service.IRedisService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName RedisController
 * @Description Redis控制器
 * @Author jaypdd
 * @Date 2020/8/12 23:10
 * @Version v1.0
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

	@Resource( name = "redisService")
	private IRedisService redisService;

	/**
	 * 添加普通key value缓存
	 * @param reqVO set缓存请求VO对象
	 * @return 添加成功返回code:0 失败code: SC_INTERNAL_SERVER_ERROR状态码
	 */
	@PostMapping("/set")
	public R saveKeyValue(@RequestBody RedisReqVO reqVO) {
		try {
			redisService.setKeyValue(reqVO.getKey(), reqVO.getValue(), reqVO.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			R.error();
		}
		return R.ok();
	}

	/**
	 * 通过key获取值
	 * @param key       key
	 * @return  获取成功返回data键值对value就是获取的对应值
	 */
	@GetMapping("/get")
	public R getValueByKey(@RequestParam("key") String key) {
		Object value = null;
		try {
			value = redisService.getValueByKey(key);
		} catch (Exception e) {
			e.printStackTrace();
			R.error();
		}
		return R.ok().put("data", value);
	}


}
